// pages/mails/addressdetails/addressdetails.js
const util = require('../../../utils/util')
Page({
      /**
       * 页面的初始数据
       */
      data: {
            nameValue: '', // 数据框中显示的值
            address: {}, // 传过来的地址
            ajaxData: [], // 搜索到的所有数据
            ajaxDataTep: [], // 用于展示的数据


            // 控制页面的数据
            hasValue: false, // 判断是否输入框有数据
            focus: true, // 自动聚焦
            id: '', // 保存用户点击地址后，保存id
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {
            let this_ = this;
            const eventChannel = this.getOpenerEventChannel()
            // 监听acceptDataFromOpenerPage事件，获取上一页面通过eventChannel传送到当前页面的数据
            eventChannel.on('acceptDataFromOpenerPage', function (res) {
                  var data = res.data
                  console.log(data)
                  this_.setData({
                        nameValue: data.title,
                        address: data,
                  })
                  this_.ajaxFn(data.title)
            })
      },

      // 初始化数据

      /* 点击一条地址 */
      clickAddress(e) {
            // console.log(e)
            var id = e.currentTarget.dataset['id']
            if (!id) return;
            var this_ = this
            this.data.ajaxData.forEach(element => {
                  // console.log(element)
                  if (element.id == id) {
                        this_.setData({
                              nameValue: element.title,
                              id: id
                        })
                        this_.ajaxFn(element.title)
                  }
            })
      },
      // 确认详细地址
      setOk() {
            let id = this.data.id;
            if(!id && this.data.nameValue){
                  wx.showToast({
                    title: '请选择一条地址',
                    icon: 'none'
                  })
                  return;
            }
            let this_ = this
            this.data.ajaxData.forEach(element => {
                  // console.log(element)
                  if (element.id == id) {
                        let data = this_.data.address;
                        data['id'] || (data['id'] = element.id)
                        data['title'] = element.title
                        data['address_components']['province'] = element.province
                        data['address_components']['city'] = element.city
                        data['address_components']['district'] = element.district
                        data['location'] = element.location
                        this_.setData({
                              address: data
                        })

                        const eventChannel = this.getOpenerEventChannel()
                        eventChannel.emit('acceptDataFromOpenedPage', {
                              data: this_.data['address']
                        });

                  }
            })
            wx.navigateBack({
                  delta: 1,
            })
      },
      // 输入框值改变
      setGoodsName(e) {
            var this_ = this;
            this.setData({
                  nameValue: e.detail.value,
                  hasValue: e.detail.value || !this.data.hasValue,
            })
            if (!e.detail.value) {
                  this_.setData({
                        ajaxDataTep: []
                  })
                  return;
            };
            this.ajaxFn(e.detail.value)
      },
      /* 获取地址的请求 */
      ajaxFn(value) {
            if (!value) return
            var this_ = this
            util.suggestion({
                  keyword: value,
                  region: "广东省",
                  success(res) {
                        let data = res.data.data;
                        // console.log(data)
                        this_.setData({
                              ajaxData: data
                        })
                        this_.gaoliang()
                  }
            })
      },

      /* 高亮 */
      gaoliang() {
            var value = this.data.nameValue;
            var data = []
            if (this.data.ajaxData.length > 0) {
                  this.data.ajaxData.forEach(element => {
                        // console.log(element)
                        if (element.title.indexOf(value.trim()) != -1) {
                              var tt_ = Object.assign({}, element)
                              tt_.title = tt_.title.replace(value, '<span class="color5">' + value + '</span>');
                              data.push(tt_);
                        }
                  });
                  this.setData({
                        ajaxDataTep: data
                  })
            }
      },

      resetSearch() {
            this.setData({
                  nameValue: '',
                  hasValue: '',
                  ajaxDataTep: []
            })
      },

})